# import tensorflow as tf
import Trans as tr
import msvcrt


class Board:
    def __init__(self):
        self.init_board = [[51, 41, 31, 21, 11, 22, 32, 42, 52],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 61, 0, 0, 0, 0, 0, 62, 0],
                           [71, 0, 72, 0, 73, 0, 74, 0, 75],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [-71, 0, -72, 0, -73, 0, -74, 0, -75],
                           [0, -61, 0, 0, 0, 0, 0, -62, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0, 0],
                           [-51, -41, -31, -21, -11, -21, -32, -42, -52]]

    def move(self, move_number):
        start = move_number // 100
        end = move_number % 100
        a = self.init_board[start % 10][start // 10]
        self.init_board[start % 10][start // 10] = 0
        self.init_board[end % 10][end // 10] = a
        return self.init_board

    def decode_move(self, move):
        # from xyab to [187]
        start = move // 100
        end = move % 100

        piece_type = self.situation[start // 10][start % 10] // 10
        delta = end - start
        count = self.same_piece_count(piece_type, start)

        if piece_type == 1:  # 帅
            dict = {1: 183, -1: 184, 10: 185, -10: 186}
            return dict[delta]

        elif piece_type == 2:  # 士
            if count == 1:
                dict = {9: 80, -9: 81, 11: 82, -11: 83}
            else:
                dict = {9: 170, -9: 171, 11: 172, -11: 173}
            return dict[delta]

        elif piece_type == 3:  # 象
            if count == 1:
                dict = {18: 76, -18: 77, 22: 78, -22: 79}
            else:
                dict = {18: 166, -18: 167, 22: 168, -22: 169}
            return dict[delta]


        elif piece_type == 4:  # 马
            if count == 1:
                dict = {8: 68, -8: 69, 12: 70, -12: 71, 19: 72, -19: 73, 21: 74, -21: 75}
            else:
                dict = {8: 158, -8: 159, 12: 160, -12: 161, 19: 162, -19: 163, 21: 164, -21: 165}
            return dict[delta]


        elif piece_type == 5:  # 车
            if count == 1:
                dict = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, -1: 8, -2: 9, -3: 10, -4: 11, -5: 12, -6: 13,
                        -7: 14, -8: 15, 10: 16, 20: 17, 30: 18, 40: 19, 50: 20, 60: 21, 70: 22, 80: 23, 90: 24, -10: 25,
                        -20: 26, -30: 27, -40: 28, -50: 29, -60: 30, -70: 31, -80: 32, -90: 33}
            else:
                dict = {1: 90, 2: 91, 3: 92, 4: 93, 5: 94, 6: 95, 7: 96, 8: 97, -1: 98, -2: 99, -3: 100, -4: 101,
                        -5: 102, -6: 103, -7: 104, -8: 105, 10: 106, 20: 107, 30: 108, 40: 109, 50: 110, 60: 111,
                        70: 112, 80: 113, 90: 114, -10: 115, -20: 116, -30: 117, -40: 118, -50: 119, -60: 120, -70: 121,
                        -80: 122, -90: 123}
            return dict[delta]

        elif piece_type == 6:  # 炮
            if count == 1:
                dict = {1: 34, 2: 35, 3: 36, 4: 37, 5: 38, 6: 39, 7: 40, 8: 41, -1: 42, -2: 43, -3: 44, -4: 45, -5: 46,
                        -6: 47, -7: 48, -8: 49, 10: 50, 20: 51, 30: 52, 40: 53, 50: 54, 60: 55, 70: 56, 80: 57, 90: 58,
                        -10: 59, -20: 60, -30: 61, -40: 62, -50: 63, -60: 64, -70: 65, -80: 66, -90: 67}

            else:
                dict = {1: 124, 2: 125, 3: 126, 4: 127, 5: 128, 6: 129, 7: 130, 8: 131, -1: 132, -2: 133, -3: 134,
                        -4: 135, -5: 136, -6: 137, -7: 138, -8: 139, 10: 140, 20: 141, 30: 142, 40: 143, 50: 144,
                        60: 145, 70: 146, 80: 147, 90: 148, -10: 149, -20: 150, -30: 151, -40: 152, -50: 153, -60: 154,
                        -70: 155, -80: 156, -90: 157}
            return dict[delta]

        elif piece_type == 7:  # 兵
            if count == 1:
                dict = {1: 84, -1: 85, 10: 86, -10: 86}
            elif count == 2:
                dict = {1: 87, -1: 88, 10: 89, -10: 89}
            elif count == 3:
                dict = {1: 180, -1: 181, 10: 182, -10: 182}
            elif count == 4:
                dict = {1: 174, -1: 175, 10: 176, -10: 176}
            else:
                dict = {1: 177, -1: 178, 10: 179, -10: 179}
            return dict[delta]

    def same_piece_count(self, piece_type, loction):
        loction_x = loction // 10
        loction_y = loction % 10
        count = 1
        for i in range(loction_x):
            for j in range(loction_y):
                if self.situation[i][j] // 100 == piece_type:
                    count += 1
        return count


a = tr.get_data()
board = Board()
# i = 0
# while 1:
#     print(board.init_board)
#     input()
#     print(a[0][0])
#     if (a[0][0]+i)%2 == 0:
#         print("胜利")
#     else:
#         print("失败")
#     board.move(a[0][1][i])
#     i += 1
